package com.woniu.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.user.commons.Result;
import com.woniu.user.controller.form.ModifyArticle;
import com.woniu.user.controller.form.PostArticleForm;
import com.woniu.user.model.Article;
import com.woniu.user.service.ArticleService;
import com.woniu.user.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @Resource
    private ArticleService articleService;

    //用户发表文章
    @PostMapping("/post")
    public Result post(@RequestBody PostArticleForm postArticleForm, HttpServletRequest req) {
        Integer userId = JwtUtil.getVenueIdFromReq(req);
        //String userType = JwtUtil.getUserTypeFromReq(req);
        log.info("{}发表文章{}",userId, postArticleForm);

        articleService.post(postArticleForm, userId);
        return Result.success(200, "发表成功", null);
    }

    //场馆发表文章
    @PostMapping("/venuePost")
    public Result venuePost(@RequestBody PostArticleForm postArticleForm, HttpServletRequest req) {
        Integer venueId = JwtUtil.getVenueIdFromReq(req);
        log.info("{}{}发表文章{}",venueId, postArticleForm);
        articleService.post(postArticleForm, venueId);
        return Result.success(200, "发表成功", null);
    }


    //查看文章
    @GetMapping("/query")
    public Result query(String condition, Integer current, Integer size) {
        log.info("查看所有文章");
        Page<Article> query = articleService.query(condition, current, size);
        return Result.success(200, "查询成功", query);
    }

    @DeleteMapping("/articleDelete/{id}")
    public Result articleDelete(@PathVariable("id") Integer id, HttpServletRequest req) {
        log.info("删除文章{}", id);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        String userType = JwtUtil.getUserTypeFromReq(req);
        articleService.articleDelete(userType, userId, id);
        return Result.success(200, "删除成功", null);
    }

    @PutMapping("/modify")
    public Result modify(ModifyArticle modifyArticle, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserIdFromReq(req);
        String userType = JwtUtil.getUserTypeFromReq(req);
        log.info("{}修改文章{}", modifyArticle);
        articleService.modify(userType, userId, modifyArticle);
        return Result.success(200, "修改成功", null);
    }

}

